What is the "this" keyword in JavaScript and how does it work?
What is the "this" keyword in JavaScript and how does it work?
530
15-May-2023
Updated on 15-May-2023
Aryan Kumar
15-May-2023The this keyword in JavaScript refers to the current context. In other words, it refers to the object that the function is being called on. The value of this can change depending on how the function is called.
For example, if a function is called on an object, this will refer to that object. If a function is called without being called on an object, this will refer to the global object.
The this keyword can be used to access properties and methods on the current object. For example, the following code will log the value of the name property on the current object:
Code snippet
The this keyword can also be used to call methods on the current object. For example, the following code will call the greet() method on the current object:
Code snippet
The this keyword is a powerful tool that can be used to access and manipulate the current object.